package com.mifengkong.cygtools.model;

import com.mifengkong.frtools.http.request.HttpFunction;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.Observer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by jiangyongxing on 17/5/5
 *
 * @author fengrong
 */

public class UploadUserAvatorModel extends BaseJJCardModel {

    public static UploadUserAvatorModel getInstance() {
        return getPresent(UploadUserAvatorModel.class);
    }

    public void execute(File file, Observer<String> observer) {
        // 创建 RequestBody，用于封装构建RequestBody
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("image/jpeg"), file);//上传图片

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);

        Observable observable = mServletApi.sendUserAvator(body).map(new HttpFunction());
        toSubscribe(observable, observer);
    }


}